//execl导入的方法  通过调用 execute 实现

import excel from 'excel-stream';
import events from 'events';
import fs from 'fs';

export default class ExcelReadExecutor extends events.EventEmitter {
    static EVENT_DATA = 'data';
    static EVENT_ERROR = 'error';
    static EVENT_CLOSE = 'close';

    /**
     * 构造方法
     * @param path 路径
     * @param options
     */

    constructor(path, options = {}) {
        this.path = path;
        this.options = options;
    }

    /**
     * 读取
     */
    execute() {
        fs.createReadStream(this.path)
            .pipe(excel(this.options))
            .on('data', (data) => this.emit(ExcelReadExecutor.EVENT_DATA, data))
            .on('error', (data) => this.emit(ExcelReadExecutor.EVENT_ERROR, data))
            .on('close', (data) => this.emit(ExcelReadExecutor.EVENT_CLOSE, data));
    }

}